package me.zhengjie.modules.system.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.System;
import me.zhengjie.modules.system.service.SystemService;
import me.zhengjie.modules.system.service.dto.SystemDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * SystemController
 *
 * @author guoyuan
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "安全溯源：系统")
@RequestMapping("/api/safeSystem")
public class SystemController {

    private final SystemService systemService;

    @Log("新增系统")
    @ApiOperation("新增系统")
    @PostMapping("/create")
    @PreAuthorize("@el.check('system:create')")
    public ResponseEntity<Object> create(@Validated @RequestBody System resources) {
        if (Objects.isNull(resources)) {
            throw new BadRequestException("System is null or empty");
        }
        systemService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("删除系统")
    @ApiOperation("删除系统")
    @DeleteMapping("/delete")
    @PreAuthorize("@el.check('system:delete')")
    public ResponseEntity<Object> delete(@RequestBody List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BadRequestException("System id list is null or empty");
        }
        systemService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("修改系统")
    @ApiOperation("修改系统")
    @PutMapping("/edit")
    @PreAuthorize("@el.check('system:edit')")
    public ResponseEntity<Object> edit(@RequestBody SystemDto resources) {
        if (Objects.isNull(resources)) {
            throw new BadRequestException("System is null or empty");
        }
        systemService.edit(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("所有系统列表查询")
    @GetMapping(value = "/query")
    @PreAuthorize("@el.check('system:query')")
    public ResponseEntity<Object> query() {
        return new ResponseEntity<>(systemService.queryAll(), HttpStatus.OK);
    }
}
